package com.peak.distribution.web.validator;

import java.util.List;

public class Validation {
	
	private ValidatorFactory factory;
	
	private Validator validator;

	public static Validation buildDefaultValidatorFactory() {
		Validation validation = new Validation();
		validation.factory = new ValidatorFactory();
		validation.validator = validation.factory.getValidator();
		return validation;
	}
	
	public Validation rule(List<ValidateRule> rules) {
		validator.setRule(rules);
		for (ValidateRule rule : rules) {
			for (Class<? extends ValidateRuleMethod> methodClass : rule.getMethods()) {
				factory.addMethod(methodClass);
			}
		}
		return this;
	}
	
	public Valid validate(Object obj) {
		validator.setValidateObject(obj);
		return validator.validate(factory.getMethodMap());
	}
}
